/**
 * 路由选择器
 *
 * @param startActivity
 * @param endActivity
 * @returns
 */
function Transition(startActivity, endActivity, startBox) {
    this.sid = context.getUuid();
    this.fromIndex = "999";
    this.startActivity = startActivity;
    this.endActivity = endActivity;
    this.startBox = startBox;
    this.endBox = null;
    this.style = {
        text: "linetest",
        stroke: '#000000',
        'stroke-width': 3,
        'arrow-end': 'classic-wide-long'
    };
    
    this.view = null;
    this.update();
    
}

Transition.prototype.update = function() {
    var path = new Array();
    if (this.view != null) {
        path = this.view.attrs.path;
        this.view.attr("path");
    }
    var sx = this.startBox.attr("x") + 3;
    var sy = this.startBox.attr("y") + 3;
    var ex = sx + 20;
    var ey = sy;
    if (this.endBox != null) {
        ex = this.endBox.attr("x") + 3;
        ey = this.endBox.attr("y") + 3;
    }
    
    path[0] = new Array();
    path[0][0] = "M";
    path[0][1] = sx;
    path[0][2] = sy;
    
    path[path.length > 1 ? path.length - 1 : path.length] = new Array();
    path[path.length - 1][0] = "L";
    path[path.length - 1][1] = ex;
    path[path.length - 1][2] = ey;
    
	var transition = this;
    if (this.view == null) {
        this.view = this.startActivity.context.paper.path(path).attr(this.style);
        this.view.data = this;
        this.view.click(function(event) {
            context.removeTransitionHotPoint();
            if (this.data.hotpoint == null && this.data.endActivity && !context.readOnly) {
                this.data.hotpoint = new PathHotPoint(this.data);
            } else {
                //this.data.hotpoint.remove();
                //this.data.hotpoint = null;
            }
			if (context.readOnly) {
				context.objClick(event, "transition", transition);
			}
        }).mouseover(function() {
            this.attr({
                'stroke-width': 6
            });
        }).mouseout(function() {
            this.attr({
                'stroke-width': 3
            });
        });
        // 路由绑定右键菜单
		if (!context.readOnly) {
			$(this.view.node).contextMenu({
				menu: 'transitionMenu'
			}, function(action, el, pos) {
				g_dx, g_dy = 0;
				if (action == "delete") {
					transition.view.remove();
					transition.removeHotPoint();
					this.context.removeTransition(transition);
				} else {
					this.context.menuClick("transition", action, transition);
				}
			});
		}
    } else {
        this.view.attr("path", path);
    }
};
Transition.prototype.updateStartXY = function(x, y) {
    if (this.view != null) {
        var path = this.view.attr("path");
        var point = new Array();
        point[0] = "M";
        point[1] = x;
        point[2] = y;
        path[0] = point;
        this.view.attr(path, path);
    }
};
Transition.prototype.updateEndXY = function(x, y) {
    if (this.view != null) {
        var path = this.view.attr("path");
        var point = new Array();
        point[0] = "L";
        point[1] = x;
        point[2] = y;
        path[path.length - 1] = point;
        this.view.attr("path", path);
    }
};

Transition.prototype.updateMove = function(id, dx, dy) {
    var path = this.view.attr("path");
    path[id][1] = path[id][1] + dx;
    path[id][2] = path[id][2] + dy;
    this.view.attr({
        "path": path
    });
    
};

Transition.prototype.moveStartXY = function(dx, dy) {
    var path = this.view.attr("path");
    path[0][1] = path[0][1] + dx;
    path[0][2] = path[0][2] + dy;
    this.view.attr("path", path);
    
};
Transition.prototype.moveEndXY = function(dx, dy) {
    var path = this.view.attr("path");
    path[path.length - 1][1] = path[path.length - 1][1] + dx;
    path[path.length - 1][2] = path[path.length - 1][2] + dy;
    this.view.attr("path", path);
};
Transition.prototype.appendPoint = function(x, y) {
    if (this.view != null) {
        var path = this.view.attr("path");
        if (path.length > 4) {
            return false;
        }
        var point = new Array();
        point[0] = "L";
        point[1] = x;
        point[2] = y;
        path[path.length] = point;
        this.view.attr("path", path);
    }
};
Transition.prototype.remove = function() {
    this.view.remove();
    this.startActivity.context.commandObject = null;
    //this.startActivity.context.command = "";
};

Transition.prototype.removeHotPoint = function() {
    if (this.hotpoint) {
        this.hotpoint.remove();
        this.hotpoint = null;
    }
};
